<?php
/**
 * Created by PhpStorm.
 * User: 虚空之翼 <183700295@qq.com>
 * Date: 16/4/27
 * Time: 11:22
 */
/**
 * 系统非常规MD5加密方法
 * @param  string $str 要加密的字符串
 * @return string
 */
function think_ucenter_md5($str, $key = 'ThinkUCenter'){
    return '' === $str ? '' : md5(sha1($str) . $key);
}

/**
 * 系统加密方法
 * @param string $data 要加密的字符串
 * @param string $key  加密密钥
 * @param int $expire  过期时间 (单位:秒)
 * @return string
 */
function think_ucenter_encrypt($data, $key, $expire = 0) {
    $key  = md5($key);
    $data = base64_encode($data);
    $x    = 0;
    $len  = strlen($data);
    $l    = strlen($key);
    $char =  '';
    for ($i = 0; $i < $len; $i++) {
        if ($x == $l) $x=0;
        $char  .= substr($key, $x, 1);
        $x++;
    }
    $str = sprintf('%010d', $expire ? $expire + time() : 0);
    for ($i = 0; $i < $len; $i++) {
        $str .= chr(ord(substr($data,$i,1)) + (ord(substr($char,$i,1)))%256);
    }
    return str_replace('=', '', base64_encode($str));
}

/**
 * 系统解密方法
 * @param string $data 要解密的字符串 （必须是think_encrypt方法加密的字符串）
 * @param string $key  加密密钥
 * @return string
 */
function think_ucenter_decrypt($data, $key){
    $key    = md5($key);
    $x      = 0;
    $data   = base64_decode($data);
    $expire = substr($data, 0, 10);
    $data   = substr($data, 10);
    if($expire > 0 && $expire < time()) {
        return '';
    }
    $len  = strlen($data);
    $l    = strlen($key);
    $char = $str = '';
    for ($i = 0; $i < $len; $i++) {
        if ($x == $l) $x = 0;
        $char  .= substr($key, $x, 1);
        $x++;
    }
    for ($i = 0; $i < $len; $i++) {
        if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
            $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
        }else{
            $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
        }
    }
    return base64_decode($str);
}
/**
 * 获取客户端IP地址
 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
 * @param boolean $adv 是否进行高级模式获取（有可能被伪装）
 * @return mixed
 */
function get_client_ip($type = 0,$adv=false) {
    $type       =  $type ? 1 : 0;
    static $ip  =   NULL;
    if ($ip !== NULL) return $ip[$type];
    if($adv){
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos    =   array_search('unknown',$arr);
            if(false !== $pos) unset($arr[$pos]);
            $ip     =   trim($arr[0]);
        }else if (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip     =   $_SERVER['HTTP_CLIENT_IP'];
        }else if (isset($_SERVER['REMOTE_ADDR'])) {
            $ip     =   $_SERVER['REMOTE_ADDR'];
        }
    }else if (isset($_SERVER['REMOTE_ADDR'])) {
        $ip     =   $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u",ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}


/**
 * 获取文档封面图片
 * @param int $cover_id
 * @param string $field
 * @return 完整的数据  或者  指定的$field字段值
 */
function get_cover($cover_id, $field = null){
    if(empty($cover_id)){
        return false;
    }
    $map = array(
        'status' => array('egt',0),
        'id' => $cover_id,
    );
    $picture = \app\home\model\Picture::where($map)->find();
    if($field == 'path'){
        if(!empty($picture['url'])){
            $picture['path'] = $picture['url'];
        }else{
            $picture['path'] = $picture['path'];
        }
    }
    return empty($field) ? $picture : $picture[$field];
}

/**
 * 统计所有数据
 * @param int $userId 用户ID3
 * @param string $event 相关统计表模型名字
 * @param int $source 相关目标的ID
 */
function save_log($userId, $event, $source = 0) {
    $department = \app\admin\model\WechatUser::where(['userid' => $userId])->value('department');
    $data = array(
        'userid' => $userId,
        'event' => $event,
        'source' => $source,
        'department_id' => json_decode($department)[0]  //TODO 获取多个部门
    );
    \app\admin\model\Log::create($data);
}
/*
 * 获取月份
 * @param str $time  时间格式
 */
function getMonth($time){
    $month = substr($time,5);
    $map = array('0','1');
    if(in_array(substr($month,0,1),$map)){
        //去掉前导零
        if(substr($month,0,1) == 0){
            return substr($month,1,1);
        }else{
            return substr($month,0,2);
        }
    }else{
        return substr($month,0,1);
    }
}
/*
 * 获取日
 */
function getDay($time){
    $num1 = strrpos($time,'.');
    if($num1 === false){
        $num2 = strrpos($time,'-');
        return substr($time,$num2+1);
    }elseif(substr($time,$num1+1)!='' && $num1 > 4){
        return substr($time,$num1+1);
    }
}

if (!function_exists('thumb')) {
    /**
     * @param string $src
     * @param int $width
     * @param int $height
     * @param int $type
     * @param bool $replace
     * @return mixed|string
     */
    function thumb($src = '', $width = 300, $height = 180, $type = 1, $replace = false)
    {
        $imgServer = config('maydie.imgserver') ?: '';

        $domain = request()->domain();
        // 默认图片
        if ($src == '' || $src == $domain) {
            return $imgServer.'/home/images/noimage.jpg';
        }
        // 过滤域名
        if(strpos($src, $domain) !== false) {
            $src = substr($src, strlen($domain));
        }

        $src = '.' .$src;
        if (is_file($src) && file_exists($src)) {
            $ext = pathinfo($src, PATHINFO_EXTENSION);
            $name = basename($src, '.' . $ext);
            $dir = dirname($src);
            if (in_array($ext, array('gif', 'jpg', 'jpeg', 'bmp', 'png'))) {
                $name = $name . '_thumb_' . $width . '_' . $height . '.' . $ext;
                $file = $dir . '/' . $name;
                if (!file_exists($file) || $replace == true) {
                    $image = \think\Image::open($src);
                    $image->thumb($width, $height, $type);
                    $image->save($file);
                }
                $file = str_replace("\\", "/", $file);
                $file = '/' . trim($file, './');
                return $imgServer.$file;
            }
        }
        $src = str_replace("\\", "/", $src);
        $src = '/' . trim($src, './');

        return $imgServer.$src;
    }
}

/**
 * 安全的异常处理函数
 * 用于替代ThinkPHP默认的异常处理
 * @param Exception $e 异常对象
 */
function secure_exception_handler($e) {
    // 获取当前是否为调试模式
    $debug = \think\Config::get('app_debug');
    
    // 记录日志
    \think\Log::record($e->getMessage() . "\n" . $e->getTraceAsString(), 'error');
    
    // 如果是API请求，返回JSON格式错误
    if (request()->isAjax() || strpos(request()->header('accept'), 'application/json') !== false) {
        header('Content-Type: application/json; charset=utf-8');
        $data = [
            'code' => $e->getCode() ?: 500,
            'msg'  => $debug ? $e->getMessage() : '服务器内部错误，请联系管理员',
            'data' => []
        ];
        
        // 在调试模式下，添加更多信息
        if ($debug) {
            $data['file'] = $e->getFile();
            $data['line'] = $e->getLine();
            $data['trace'] = explode("\n", $e->getTraceAsString());
        }
        
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
        exit;
    }
    
    // 普通请求，显示错误页面
    if ($e instanceof \think\exception\HttpException) {
        $statusCode = $e->getStatusCode();
    } else {
        $statusCode = 500;
    }
    
    // 设置HTTP状态码
    http_response_code($statusCode);
    
    // 根据状态码显示不同的错误页面
    if ($statusCode == 404) {
        include APP_PATH . 'admin/view/base/404.html';
    } else {
        include APP_PATH . 'admin/view/base/500.html';
    }
    
    exit;
}

/**
 * 安全过滤UEditor内容
 * @param string $html UEditor生成的HTML内容
 * @return string 过滤后的HTML
 */
function filter_ueditor_content($html) {
    // 移除所有JavaScript事件属性
    $html = preg_replace('/\s+on\w+=([\'"]).*?\1/i', '', $html);

    // 移除所有<script>标签
    $html = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', '', $html);

    // 移除所有javascript:协议
    $html = preg_replace('/javascript:[^\'"]+/i', '', $html);

    // 移除所有iframe标签
    $html = preg_replace('/<iframe\b[^>]*>(.*?)<\/iframe>/is', '', $html);

    // 移除危险的style属性值
    $html = preg_replace('/expression\s*\([^)]*\)/i', '', $html);
    $html = preg_replace('/behavior\s*:[^;"]*/i', '', $html);

    return $html;
}
